package com.charity.generator.vo.model;

import com.charity.generator.util.StringUtils;
import lombok.Data;

import java.util.Date;


@Data
public class GenTableColumnModel {

    private Long id;

    private Long tableId;

    private String columnName;

    private String columnComment;

    private String columnType;

    private String javaType;

    private String javaField;

    private Boolean isPk;

    private Boolean isIncrement;

    private Boolean isRequired;

    private Boolean isInsert;

    private Boolean isEdit;

    private Boolean isList;

    private Boolean isQuery;

    private String queryType;

    private String htmlType;

    private String dictType;

    private Integer sort;

    private Long createId;

    private Date createTime;

    private Long updateId;

    private Date updateTime;

    private String operateName;

    public boolean isSuperColumn() {
        return isSuperColumn(this.javaField);
    }

    public static boolean isSuperColumn(String javaField) {
        return StringUtils.equalsAnyIgnoreCase(javaField,
                // BaseEntity
                "createBy" , "createTime" , "updateBy" , "updateTime" , "remark" ,
                // TreeEntity
                "parentName" , "parentId" , "sortNum" , "ancestors");
    }

    public boolean isUsableColumn() {
        return isUsableColumn(javaField);
    }

    public static boolean isUsableColumn(String javaField) {
        // isSuperColumn()中的名单用于避免生成多余Domain属性，若某些属性在生成页面时需要用到不能忽略，则放在此处白名单
        return StringUtils.equalsAnyIgnoreCase(javaField, "parentId" , "sortNum" , "remark");
    }

    public String readConverterExp() {
        String remarks = StringUtils.substringBetween(this.columnComment, "（" , "）");
        StringBuffer sb = new StringBuffer();
        if (StringUtils.isNotEmpty(remarks)) {
            for (String value : remarks.split(" ")) {
                if (StringUtils.isNotEmpty(value)) {
                    Object startStr = value.subSequence(0, 1);
                    String endStr = value.substring(1);
                    sb.append("").append(startStr).append("=").append(endStr).append(",");
                }
            }
            return sb.deleteCharAt(sb.length() - 1).toString();
        } else {
            return this.columnComment;
        }
    }
}
